package com.winfield.photovideo;

import java.io.File;

import com.location.LocateFunc;
import com.winfield.photovideo.R;
import com.winfield.service.NewsService;
import com.winfield.utils.MyThumbnail;

import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 上传界面
 * @author Winfield
 *
 */
public class UploadActivity extends Activity {

	private EditText pvDescribeText;		//资源描述
	private EditText pvAddressText;			//地址
	private EditText pvTimeText;			//时间
	private EditText nameText;				//地址
	private ImageView imageView;			//存放资源的imageView  用于显示资源（照片或视频）
	private  String filePath;				//所拍摄资源所存放路径
	private  String fileName;				//所拍摄资源的名字
	private  String userID;					//登陆用户的ID
	private  View videoplayimageview;			//视频上的那个箭头
	private LocateFunc locatefunc;
	private Handler mhandler;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uploadactivity);
        
        
        
        pvDescribeText = (EditText)this.findViewById(R.id.pvdescribe);
        pvAddressText = (EditText)this.findViewById(R.id.pvaddress);
        pvTimeText = (EditText)this.findViewById(R.id.pvtime);
        imageView = (ImageView)this.findViewById(R.id.imageView);
        videoplayimageview = this.findViewById(R.id.videoplayimageview);
       
        //异步定位
       locate();
        
       //显示缩略图
       showThumbnail();
        
  
		
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void save(View view){
    	//String filename = nameText.getText().toString();
    	//TODO
    	//Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/1.jpg");
        
       // imageView.setImageBitmap(bitmap);//显示图片
    	
    	String pvDescribe = pvDescribeText.getText().toString();
    	String pvAddress = pvAddressText.getText().toString();
    	String pvTime = pvTimeText.getText().toString();
    	
    	//本应用时默认在sd卡的根目录下寻找该文件
    	//1.判断sd卡的状态，是可读写，还是只读不可以写
    	if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)||
    			Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
    		//取得sdk的根目录  然后指定要上传的文件的名称
    		File uploadFile = new File(filePath,fileName);
    		
    		if(uploadFile.exists()){
    			//文件存在  则上传文件
    			boolean result = NewsService.save(userID,pvDescribe,pvAddress,pvTime,uploadFile);
            	if(result){
            		Toast.makeText(getApplicationContext(), R.string.uploadsuccess, 1).show();
            	}else{
            		Toast.makeText(getApplicationContext(), R.string.uploaderror, 1).show();
            	}
    			
    			
    		}else{
    			//如果文件不存在  则给用户提示相应的信息
    			Toast.makeText(getApplicationContext(), R.string.filenoexit, 1).show();
    		}
    	}else{
    		//如果sd卡不存在（实际上不是可读的） 就直接保存两个文本信息
    		boolean result = NewsService.save(userID,pvDescribe,pvAddress,pvTime);
        	if(result){
        		Toast.makeText(getApplicationContext(), R.string.uploadsuccess, 1).show();
        	}else{
        		Toast.makeText(getApplicationContext(), R.string.uploaderror, 1).show();
        	}
    	}	
    	
    }
    
    /**
     * 该方法用于实现定位
     */
    public void locate(){
    	 //异步处理地址
  		mhandler = new Handler() {
  			public void handleMessage(Message msg) {
  				switch (msg.what) {
  				case 0: {
  					//显示地址
  					pvAddressText.setText((msg.obj).toString());	
  					break;
  				}
  				case 1: {
  					//显示时间
  					pvTimeText.setText((msg.obj).toString());					
  					break;
  				}
  				}
  				//Toast.makeText(getApplicationContext(), "winfield", 1).show();
  			}
  		};
  		locatefunc = new LocateFunc(this.getApplicationContext(), mhandler);
  		locatefunc.requestlocation();
    }
    
    /**
     * 该方法用于实现生成缩略图并显示到ImageView上
     */
    public void showThumbnail(){
    	//下面代买实现显示缩略图
        //intent是用于激活它的对象
      	Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        filePath = bundle.getString("filePath");
        fileName = bundle.getString("fileName");
        userID = bundle.getString("userID");
        Toast.makeText(getApplicationContext(), filePath+"/"+fileName, 1).show();
       
        Log.e("I am a userid", new Integer(userID).toString());
        
        Bitmap bitmap;//缩略图
        //将资源显示在ImageView上
        if(fileName.endsWith("3gp")){
        	//要上传的资源是视频  3gp格式
        	//bitmap = ThumbnailUtils.createVideoThumbnail(filePath+"/"+fileName, MediaStore.Images.Thumbnails.MICRO_KIND);
        	bitmap = MyThumbnail.getVideoThumbnail(filePath+"/"+fileName, 100, 60, MediaStore.Images.Thumbnails.MINI_KIND);
        	imageView.setImageBitmap(bitmap);//显示图片
        	
        	//视频上显示一个play图标出来
        	videoplayimageview.setVisibility(ViewGroup.VISIBLE);
        }else{
        	//要上传的资源师照片  jpg格式
        	bitmap = MyThumbnail.getImageThumbnail(filePath+"/"+fileName, 100, 60);
        	imageView.setImageBitmap(bitmap);//显示图片
        	//bitmap = BitmapFactory.decodeFile(filePath+"/"+fileName);
        }
       
    }
    
}
